<template>
  <!-- <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider> -->
  <a-modal :visible="open" :width="850" :title="formTitle" @cancel="onClose">
    <a-form-model ref="form" :label-col="{ span: 7 }" :wrapper-col="{ span: 15 }" :model="form" :rules="rules">
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="品牌" prop="carBrand">
            <a-input v-model="form.carBrand" placeholder="请输入品牌" :maxLength="30" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="车型" prop="carType">
            <a-select v-model="form.carType" placeholder="请选择车型" :filterOption="filterOption" show-search>
              <a-select-option v-for="(d, index) in carTypeList" :key="index" :value="d.id.toString()">{{ d.info
                }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="车牌号" prop="carNo">
            <a-input v-model="form.carNo" placeholder="请输入车牌号" :maxLength="10" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="行驶证号" prop="carLicenseNo">
            <a-input v-model="form.carLicenseNo" placeholder="请输入行驶证号" :maxLength="20" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="司机" prop="driverIdList">
            <a-select mode="multiple" v-model="form.driverIdList" placeholder="请选择司机" :filterOption="filterOption">
              <a-select-option v-for="(d, index) in driverTypeList" :key="index" :value="d.id.toString()">{{
                d.driverName
                }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="状态" prop="status">
            <a-radio-group v-model="form.status" button-style="solid">
              <a-radio-button v-for="(d, index) in statusOptions" :key="index" :value="d.value">{{ d.label
                }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col> </a-row> <a-row>
        <a-col :span="12">
          <a-form-model-item label="行驶证照片" prop="carLicenseImgUrl">
            <!--        <a-input v-model="form.carLicenseImgUrl" placeholder="请输入行驶证照片" />-->
            <base-upload v-model="form.carLicenseImgUrl"></base-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <template slot="footer">
      <a-space>
        <a-button type="primary" :loading="submitLoading" @click="submitForm">
          保存
        </a-button>
        <a-button type="dashed" @click="cancel">
          取消
        </a-button>
      </a-space>
    </template>
  </a-modal>
  <!-- </a-drawer> -->
</template>

<script>
import { getInfo, addInfo, updateInfo, getDriverlist, getCarTypelist } from '@/api/vehicle/carinfo'
import { getDicts } from '@/api/system/dict/data'
import baseUpload from '@/components/imgUpload/index.vue'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
  },
  components: { baseUpload },
  data() {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        carBrand: null,

        carType: undefined,

        carNo: null,

        carLicenseNo: null,

        carLicenseMinUrl: null,

        carLicenseImgUrl: null,

        status: '0',
        driverIdList: undefined
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        carType: [
          { required: true, message: '车型不能为空', trigger: 'change' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        carLicenseImgUrl: [
          { required: true, message: '行驶证照片不能为空', trigger: 'blur' }
        ],
        carNo: [
          { required: true, message: '车牌号不能为空', trigger: 'blur' }
        ],
        driverIdList: [
          { required: true, message: '司机不能为空', trigger: 'blur' }
        ],
        // carLicenseNo: [
        //   { required: true, message: '行驶证号不能为空', trigger: 'blur' }
        // ],
      },
      carTypeList: [], //车型字典
      carStatusList: [], //车辆状态字典
      driverTypeList: [], //司机列表
    }
  },
  filters: {},
  created() {
    this.getOptionDic()
  },
  computed: {},
  watch: {},
  mounted() {
  },
  methods: {
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formType = 1
      this.form = {
        id: null,

        carBrand: null,

        carType: undefined,

        carNo: null,

        carLicenseNo: null,

        carLicenseMinUrl: null,

        carLicenseImgUrl: null,

        status: '0',

        delFlag: '0',

        driverIdList: undefined

      }
      this.$refs.form?.resetFields()
    },
    /** 获取字典 */
    getOptionDic() {
      //获取车型字典
      getCarTypelist().then(res => {
        this.carTypeList = res.data
      })
      //车辆状态
      getDicts('sys_normal_disable').then(res => {
        this.carStatusList = res.data
      })
      getDriverlist().then(res => {
        this.driverTypeList = res.data
      })
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    // 筛选功能
    filterOption(inputValue, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(inputValue.toLowerCase()) >= 0
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateInfo(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addInfo(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
